home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / xlib / xswap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  8.6 KB  |  394 lines

  1. /*
  2.  * Copyright 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <X11/Xlib.h>
  18. #include <X11/Xutil.h>
  19. #include <GL/glx.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <X11/keysym.h>
  23.  
  24. static int RGBattributes_DB[] = {
  25.     GLX_RGBA,
  26.     GLX_RED_SIZE, 1,
  27.     GLX_GREEN_SIZE, 1,
  28.     GLX_BLUE_SIZE, 1,
  29.     GLX_DOUBLEBUFFER,
  30.     None,
  31. };
  32.  
  33. static int CIattributes_DB[] = {
  34.     GLX_DOUBLEBUFFER,
  35.     None,
  36. };
  37.  
  38. enum {
  39.     BLACK = 0,
  40.     RED,
  41.     GREEN,
  42.     YELLOW,
  43.     BLUE,
  44.     MAGENTA,
  45.     CYAN,
  46.     WHITE
  47. };
  48.  
  49. #define COLOR_OFFSET_1    16
  50. #define COLOR_OFFSET_2    32
  51.  
  52.  
  53. static float rgbMap[][3] = {
  54.     {0, 0, 0},
  55.     {1, 0, 0},
  56.     {0, 1, 0},
  57.     {1, 1, 0},
  58.     {0, 0, 1},
  59.     {1, 0, 1},
  60.     {0, 1, 1},
  61.     {1, 1, 1}
  62. };
  63.  
  64. static long W = 300, H = 300;
  65. static Display *dpy;
  66. static Window window;
  67. static Colormap cmap;
  68. int drawX = 1;
  69. int drawGL = 1;
  70. int doubleBuf = 1;
  71. int swaps = 1;
  72. GLenum drawBuffer = GL_FRONT;
  73. static long rgb;
  74. static long ci1 = BLUE, ci2 = GREEN;
  75.  
  76.  
  77.  
  78. static GLuint LoadGLFont(void)
  79. {
  80.     XFontStruct *fontInfo;
  81.     Font id;
  82.     GLuint base;
  83.     int first, last;
  84.  
  85.     static char pattern1[] = "-adobe-courier-bold-o-normal--18-*-*-*-*-*-*-*";
  86.  
  87.     fontInfo = XLoadQueryFont(dpy, pattern1);
  88.  
  89.     id = fontInfo->fid;
  90.     first = (int)fontInfo->min_char_or_byte2;
  91.     last = (int)fontInfo->max_char_or_byte2;
  92.  
  93.  
  94.     base = glGenLists(last+1);
  95.     if (base == 0) {
  96.         return 0;
  97.     }
  98.     glXUseXFont(id, first, last-first+1, (int)(base+first));
  99.     return base;
  100. }
  101.  
  102.  
  103. static void Usage(void)
  104. {
  105.     printf("Usage: xswap [-c]\n");
  106.     printf("   -c:  Run in color index mode\n");
  107.     exit(-1);
  108. }
  109.  
  110. static GLint DrawXFont(GLint x, GLint y, char *string, GLint len)
  111. {
  112.     static char pattern1[] = "-adobe-courier-bold-o-normal--18-*-*-*-*-*-*-*";
  113.     static char pattern2[] = "-*-*-*-*-*-*-18-*-*-*-*-*-*-*";
  114.     static char size[] = "456781";
  115.     GC gc;
  116.     XGCValues values;
  117.     XFontStruct *fontInfo;
  118.     char **fontList;
  119.     XColor exact, green;
  120.     unsigned long mask;
  121.     int count, i;
  122.  
  123.     glXWaitGL();
  124.  
  125.     fontList = XListFonts(dpy, pattern1, 1, &count);
  126.     if (count == 0) {
  127.         for (i = 0; i < 6; i++) {
  128.             pattern2[14] = size[i];
  129.             fontList = XListFonts(dpy, pattern2, 1, &count);
  130.             if (count > 0) {
  131.                 break;
  132.             }
  133.         }
  134.     }
  135.     if (count == 0) {
  136.        return GL_FALSE;
  137.     }
  138.  
  139.     fontInfo = XLoadQueryFont(dpy, fontList[0]);
  140.     if (fontInfo == NULL) {
  141.         return GL_FALSE;
  142.     }
  143.  
  144.     mask = GCForeground | GCFont;
  145.     values.font = fontInfo->fid;
  146.  
  147.     if (rgb) {
  148.         XAllocNamedColor(dpy, cmap, "Green", &exact, &green);
  149.         values.foreground = green.pixel;
  150.     } else {
  151.         values.foreground = 2;
  152.     }
  153.  
  154.     gc = XCreateGC(dpy, window, mask, &values);
  155.     XDrawString(dpy, window, gc, (int)x, (int)y, string, (int)len);
  156.  
  157.     XFreeFontNames(fontList);
  158.     glXWaitX();
  159.     return GL_TRUE;
  160. }
  161.  
  162. static void DoDisplay(void)
  163. {
  164.     glLoadIdentity();
  165.     glOrtho(0.0, W, 0.0, H, -0.5, 1000.0);
  166.  
  167.     if (swaps) {
  168.     glDrawBuffer(GL_BACK);
  169.     glClearColor(0.0, 0.0, 1.0, 0.0);
  170.     glClearIndex(2);
  171.     glClear(GL_COLOR_BUFFER_BIT);
  172.     glDrawBuffer(GL_FRONT);
  173.     glClearColor(0.0, 0.0, 0.0, 0.0);
  174.     glClearIndex(0);
  175.     glClear(GL_COLOR_BUFFER_BIT);
  176.     } else {
  177.     glDrawBuffer(GL_BACK);
  178.     glClearColor(0.0, 0.0, 0.0, 0.0);
  179.     glClearIndex(0);
  180.     glClear(GL_COLOR_BUFFER_BIT);
  181.     glDrawBuffer(GL_FRONT);
  182.     glClearColor(0.0, 0.0, 1.0, 0.0);
  183.     glClearIndex(2);
  184.     glClear(GL_COLOR_BUFFER_BIT);
  185.     }
  186.  
  187.     glDrawBuffer(drawBuffer);
  188.  
  189.     if (drawGL) {
  190.     glRasterPos2f(10.0, 10.0);
  191.     glCallLists(17, GL_UNSIGNED_BYTE, (unsigned char *)"this is a GL font");
  192.     glXWaitGL();
  193.     }
  194.     if (drawX) {
  195.     DrawXFont(10,40, "this is an X font", 17);
  196.     glXWaitX();
  197.     }
  198.  
  199.     if (drawBuffer == GL_FRONT)
  200.         printf("drawGL=%d, drawX=%d, drawbuffer=GL_FRONT\n",drawGL, drawX);
  201.     else
  202.         printf("drawGL=%d, drawX=%d, drawbuffer=GL_BACK\n",drawGL, drawX);
  203.     glFlush();
  204. }
  205.  
  206. static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
  207. {
  208.     if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
  209.     return GL_TRUE;
  210.     }
  211.     return GL_FALSE;
  212. }
  213.  
  214. int main(long argc, char** argv)
  215. {
  216.     XVisualInfo *vi;
  217.     XSetWindowAttributes swa;
  218.     GLXContext cx;
  219.     XEvent event;
  220.     GLboolean needDisplay;
  221.     int i;
  222.     GLuint base;
  223.  
  224.     rgb = 1;
  225.     doubleBuf = 1;
  226.     for (i = 1; i < argc; i++) {
  227.         if (argv[i][0] == '-') {
  228.             switch (argv[i][1]) {
  229.               case 'c':
  230.                 rgb = 0;
  231.                 break;
  232.               default:
  233.                 Usage();
  234.             }
  235.         } else {
  236.             Usage();
  237.         }
  238.     }
  239.  
  240.     dpy = XOpenDisplay(0);
  241.     if (!dpy) {
  242.     fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
  243.     return -1;
  244.     }
  245.  
  246.     vi = glXChooseVisual(dpy, DefaultScreen(dpy),
  247.              (rgb ? RGBattributes_DB : CIattributes_DB));
  248.     if (!vi) {
  249.     fprintf(stderr, "No appropriate visual on \"%s\"\n",
  250.         getenv("DISPLAY"));
  251.     return -1;
  252.     }
  253.  
  254.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
  255.                rgb ? AllocNone : AllocAll);
  256.     if (!rgb) {
  257.         XColor buf;
  258.         int i;
  259.  
  260.         buf.flags = DoRed | DoGreen | DoBlue;
  261.  
  262.         /* Init color map */
  263.         for (i=0; i<16; i++) {
  264.             buf.pixel = i;
  265.             buf.blue = (i & 4) ? 65535 : 0;
  266.             buf.green = (i & 2) ? 65535 : 0;
  267.             buf.red = (i & 1) ? 65535 : 0;
  268.             if (i > 8) {
  269.                 buf.red /= 2;
  270.                 buf.green /= 2;
  271.                 buf.blue /= 2;
  272.             }
  273.             XStoreColor(dpy, cmap, &buf);
  274.         }
  275.     }
  276.  
  277.  
  278.     swa.border_pixel = 0;
  279.     swa.colormap = cmap;
  280.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
  281.     | KeyReleaseMask;
  282.     window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
  283.                W, H,
  284.                0, vi->depth, InputOutput, vi->visual,
  285.                CWBorderPixel|CWColormap|CWEventMask, &swa);
  286.     XSetWMColormapWindows(dpy, window, &window, 1);
  287.     XMapWindow(dpy, window);
  288.     XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
  289.  
  290.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  291.     if (!glXMakeCurrent(dpy, window, cx)) {
  292.     fprintf(stderr, "Can't make window current to context\n");
  293.     return -1;
  294.     }
  295.  
  296. /****
  297.     if (!rgb) {
  298.     XColor xc[32+2];
  299.  
  300.     unsigned long pixel;
  301.     unsigned short red, green, blue;
  302.     char flags;  
  303.     char pad;
  304.  
  305.     for (i = 0; i < 16; i++) {
  306.         xc[i].pixel = i + COLOR_OFFSET_1;
  307.         xc[i].red = 0;
  308.         xc[i].green = 0;
  309.         xc[i].blue = (unsigned short) (65535.0 * (i / 15.0));
  310.         xc[i].flags = DoRed | DoGreen | DoBlue;
  311.  
  312.         xc[i+16].pixel = i + COLOR_OFFSET_2;
  313.         xc[i+16].red = 0;
  314.         xc[i+16].green = (unsigned short) (65535.0 * (i / 15.0));
  315.         xc[i+16].blue = 0;
  316.         xc[i+16].flags = DoRed | DoGreen | DoBlue;
  317.     }
  318.  
  319.     xc[32].pixel = BLUE;
  320.     xc[32].red = xc[32].green = 0;
  321.     xc[32].blue = 65535;
  322.     xc[32].flags = DoRed|DoGreen|DoBlue;
  323.  
  324.     xc[32].pixel = GREEN;
  325.     xc[32].red = xc[32].blue = 0;
  326.     xc[32].green = 65535;
  327.     xc[32].flags = DoRed|DoGreen|DoBlue;
  328.     XStoreColors(dpy, cmap, xc, 34);
  329.     xswap
  330. ****/
  331.  
  332.     base = LoadGLFont();
  333.     glListBase(base);
  334.  
  335.     needDisplay = GL_TRUE;
  336.     for (;;) {
  337.     do {
  338.         XNextEvent(dpy, &event);
  339.         switch (event.type) {
  340.           case Expose:
  341.         needDisplay = GL_TRUE;
  342.         break;
  343.           case ConfigureNotify:
  344.         W = event.xconfigure.width;
  345.         H = event.xconfigure.height;
  346.         needDisplay = GL_TRUE;
  347.         break;
  348.           case KeyPress:
  349.         {
  350.             char buf[100];
  351.             int rv;
  352.             KeySym ks;
  353.  
  354.             rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
  355.             switch (ks) {
  356.               case XK_f:
  357.             drawBuffer = GL_FRONT;
  358.             needDisplay = GL_TRUE;
  359.             break;
  360.               case XK_b:
  361.             drawBuffer = GL_BACK;
  362.             needDisplay = GL_TRUE;
  363.             break;
  364.               case XK_g:
  365.             drawGL = !drawGL;
  366.             needDisplay = GL_TRUE;
  367.             break;
  368.               case XK_x:
  369.             drawX = !drawX;
  370.             needDisplay = GL_TRUE;
  371.             break;
  372.               case XK_r:
  373.             needDisplay = GL_TRUE;
  374.             break;
  375.               case XK_S:
  376.               case XK_s:
  377.             glXSwapBuffers(dpy, window);
  378.             swaps = !swaps;
  379.             break;
  380.               case XK_Escape:
  381.             return 0;
  382.             }
  383.         }
  384.         break;
  385.         }
  386.     } while (XPending(dpy) != 0);
  387.  
  388.     if (needDisplay) {
  389.         needDisplay = GL_FALSE;
  390.         DoDisplay();
  391.     }
  392.     }
  393. }
  394.